package com.springboot01.controller;

import com.springboot01.service.EmployeeService;
import entity.EmployeeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
//add
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(HttpServletRequest request){
        int id=Integer.parseInt(request.getParameter("id"));
        String name=request.getParameter("name");
        String title=request.getParameter("title");
        EmployeeEntity emp=new EmployeeEntity();
        emp.setId(id);
        emp.setName(name);
        emp.setTitle(title);
        employeeService.save(emp);
        return "add";
    }
    //all
    @GetMapping("/all")
    public List<EmployeeEntity> getAll(Model model){
        model.addAttribute("employeelist", employeeService.findAll());
        return employeeService.findAll();
    }
    //delete
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable int id){
        employeeService.delete(id);
        return "delete success";
    }
    //查
    @GetMapping("/info")
    public EmployeeEntity findOne(HttpServletRequest request){
        int id=Integer.parseInt(request.getParameter("id"));
        return employeeService.findEmployeeById(id);
    }

}
